<?php
namespace Admin\Controller;
use Think\Controller;
class FeijiUserController extends PublicController{
	//***********************************************
    public static $Array;//这个给检查产品的字段用 
    public static $PRO_FENLEI; //这个给产品分类打勾用
	//**************************************************
	//**********************************************
	//说明：申请包机列表
	//**********************************************
	public function baolist(){

		//搜索
		$where="1=1 AND del<1 ";
		
		$count=M('bao_ji')->where($where)->count();
		define('rows',5);
		$rows=ceil($count/rows);
		$page=(int)$_GET['page'];
		$page<0?$page=0:'';
		$limit=$page*rows;
		$page_index=$this->page_index($count,$rows,$page);
		$productlist=M('bao_ji')->where($where)->order('addtime desc')->limit($limit,rows)->select();
		$data = $this->ariport_xq();
		foreach ($productlist as $k => &$vr) {
			$value['start_place'] = $vr['start_place'];
			$value['end_place'] = $vr['end_place'];
			$vr['start_place'] = $data[$value['start_place']]['address'];
			$vr['end_place'] = $data[$value['end_place']]['address'];
			$vr['start_airport'] = $data[$value['start_place']]['airport_name'];
			$vr['end_airport'] = $data[$value['end_place']]['airport_name'];
			$vr['addtime'] = date('Y-m-d H:i:s',$vr['addtime']);
			$vr['start_time'] = date('Y-m-d H:i:s',$vr['start_time']);
			if($vr['status'] ==1){
				$vr['statusd'] = '通过';
			}else{
				$vr['statusd'] = '未审核';
			}
			if($vr['usertype'] ==1){
				$vr['usertype'] = '单程';
			}elseif($vr['usertype'] ==2){
				$vr['usertype'] = '往返';
			}elseif($vr['usertype'] ==3){
				$vr['usertype'] = '多程';
			}
			if($vr['isfood'] ==0){
				$vr['isfood'] = '否';
			}elseif($vr['isfood'] ==1){
				$vr['isfood'] = '中餐';
			}elseif($vr['isfood'] ==2){
				$vr['isfood'] = '西餐';
			}
			if($vr['feiji_help'] ==0){
				$vr['feiji_help'] = '否';
			}elseif($vr['feiji_help'] ==1){
				$vr['feiji_help'] = '接';
			}elseif($vr['feiji_help'] ==2){
				$vr['feiji_help'] = '送';
			}elseif($vr['feiji_help'] ==3){
				$vr['feiji_help'] = '接送';
			}
		}

		//==========================
		// 将GET到的数据再输出
		//==========================
		if($_GET['test'] ==1){
			print_r($productlist);die;
		}
		
		//=============
		// 将变量输出
		//=============	
		$this->assign('baolist',$productlist);
		$this->assign('page_index',$page_index);
		$this->display();
	}
	//包机配置
	public function bao_mod(){
		$id = $_GET['id'];//包机申请ID
		$feiji_type = $_POST['feiji_type'];//飞机型号
		$start_airport = $_POST['start_airport'];//出发机场
		$end_airport = $_POST['end_airport'];//到达机场
		$start_lou = $_POST['start_lou'];//出发航站楼
		$end_lou = $_POST['end_lou'];//到达航站楼
		$deposit = $_POST['deposit'];//定金
		$preferential_price = $_POST['preferential_price'];//优惠价
		$original_price = $_POST['original_price'];//原价
		$seat = $_POST['seat'];//座位数
		$isfood = $_POST['isfood'];//是否餐饮 0 否 1中餐 2西餐
		$feiji_help = $_POST['feiji_help'];//是否接机 0否 1接 2送 3接送
		$number = $_POST['number'];//人数
		$fei_time = $_POST['fei_time'];//飞行时间
		//查询该包机信息
		$baoji_xq = M('bao_ji')->where('id='.intval($id))->find();
		$baoji_xq['start_time'] = date('Y-m-d H:i:s',$baoji_xq['start_time']);
		$baoji_xq['end_time'] = date('Y-m-d H:i:s',$baoji_xq['end_time']);
		//获取机场信息
		$data = $this->ariport_xq();
		$baoji_xq['start_airport'] = $data[$baoji_xq['start_place']]['airport_name'];
		$baoji_xq['end_airport'] = $data[$baoji_xq['end_place']]['airport_name'];
		$baoji_xq['start_place'] = $data[$baoji_xq['start_place']]['address'];
		$baoji_xq['end_place'] = $data[$baoji_xq['end_place']]['address'];
			if($baoji_xq['usertype'] ==1){
				$baoji_xq['usertype'] = '单程';
			}elseif($baoji_xq['usertype'] ==2){
				$baoji_xq['usertype'] = '返程';
			}elseif($baoji_xq['usertype'] ==3){
				$baoji_xq['usertype'] = '多程';
			}
			if($baoji_xq['isfood'] ==0){
				$baoji_xq['isfood'] = '否';
			}elseif($baoji_xq['isfood'] ==1){
				$baoji_xq['isfood'] = '中餐';
			}elseif($baoji_xq['isfood'] ==2){
				$baoji_xq['isfood'] = '西餐';
			}
			if($baoji_xq['feiji_help'] ==0){
				$baoji_xq['feiji_help'] = '否';
			}elseif($baoji_xq['feiji_help'] ==1){
				$baoji_xq['feiji_help'] = '接';
			}elseif($baoji_xq['feiji_help'] ==2){
				$baoji_xq['feiji_help'] = '送';
			}elseif($baoji_xq['feiji_help'] ==3){
				$baoji_xq['feiji_help'] = '接送';
			}
		//查询所有机型
		$where = "1=1 and del <1";
		$Feijidata = M('feiji_type')->where($where)->select();
		
		//编辑配置信息
		if($_POST['submit']==true){
			
			$data = array(
				'feiji_type' => $feiji_type,
				// 'start_airport' => $start_airport,
				// 'end_airport' => $end_airport,
				'start_lou' => $start_lou,
				'end_lou' => $end_lou,
				'deposit' => $deposit,
				'preferential_price' => $preferential_price,
				'original_price' => $original_price,
				'seat' => $seat,
				'fei_time' => $fei_time,
				'updatetime' => time(),
			);
			// print_r($data);die;
			$up = M('bao_ji')->where('id='.intval($id))->save($data);

			//规格操作
			if($up){
				$this->redirect('baolist',array('page'=>intval($_REQUEST['page'])));
				exit();
			}else{
				throw new \Exception('操作失败.');
			}
			  
		}
		$this->assign('Feijidata',$Feijidata);
		$this->assign('baoji_xq',$baoji_xq);
		$this->display();
	}
	//包机订单列表
	public function bao_orderlist(){
		//搜索
		$where="1=1 AND del<1 ";
		
		$count=M('bao_ji_order')->where($where)->count();
		define('rows',5);
		$rows=ceil($count/rows);
		$page=(int)$_GET['page'];
		$page<0?$page=0:'';
		$limit=$page*rows;
		$page_index=$this->page_index($count,$rows,$page);
		$productlist=M('bao_ji_order')->where($where)->order('addtime desc')->limit($limit,rows)->select();
		// dump($productlist);exit;
		foreach ($productlist as $k => &$vr) {
			$vr['addtime'] = date('Y-m-d H:i:s',$vr['addtime']);
			$vr['start_time'] = date('Y-m-d H:i:s',$vr['start_time']);
			$vr['end_time'] = date('Y-m-d H:i:s',$vr['end_time']);
			if($vr['status'] ==1){
				$vr['status'] = '已付';
			}else{
				$vr['status'] = '未付';
			}
			if($vr['type'] ==1){
				$vr['type'] = '微信';
			}
		}

		//==========================
		// 将GET到的数据再输出
		//==========================
		if($_GET['test'] ==1){
			print_r($productlist);die;
		}
		
		//=============
		// 将变量输出
		//=============	
		$this->assign('bao_orderlist',$productlist);
		$this->assign('page_index',$page_index);
		$this->display();
	}
	//包机审核
	public function bao_upstatus(){
		$id = $_GET['did'];
		$data = array(
			'status' => 1,
		);
		$up = M('bao_ji')->where('id='.intval($id))->save($data);
		if ($up) {
			$this->redirect('baolist',array('page'=>intval($_REQUEST['page'])));
			exit();
		}else{
			$this->error('操作失败.');
			exit();
		}
	}
	//拼机审核
	public function pin_upstatus(){
		$id = $_GET['did'];
		$data = array(
			'del' => 1,
		);
		$up = M('ping_ji')->where('id='.intval($id))->save($data);
		if ($up) {
			$this->redirect('pinglist',array('page'=>intval($_REQUEST['page'])));
			exit();
		}else{
			$this->error('操作失败.');
			exit();
		}
	}
	//【拼机配置
	public function ping_mod(){
		$id = $_REQUEST['id'];//拼机申请ID
		$feiji_type = $_POST['feiji_type'];//飞机型号
		$start_place = $_POST['start_place'];//出发机场
		$end_place = $_POST['end_place'];//到达机场
		$start_lou = $_POST['start_lou'];//出发航站楼
		$end_lou = $_POST['end_lou'];//到达航站楼
		$seat = $_POST['seat'];//座位数
		$money = $_POST['money'];//金额
		$number = $_POST['number'];//人数
		$fei_time = $_POST['fei_time'];//飞行时间
		$mudi = $_POST['mudi'];
		$start_time = $_POST['start_time'];
		
		//查询所有机型
		$where = "1=1 and del <1";
		$Feijidata = M('feiji_type')->where($where)->select();
		
		//编辑配置信息
		if($_POST['submit']==true){
			
			$data = array(
				'feiji_type' => $feiji_type,
				'start_place' => $start_place,
				'end_place' => $end_place,
				'start_airport' => $start_place,
				'end_airport' => $end_place,
				'start_lou' => $start_lou,
				'end_lou' => $end_lou,
				'money' => $money,
				'seat' => $seat,
				'number' => $number,
				'fei_time' => $fei_time,
				'mudi' => $mudi,
				'start_time' => strtotime($start_time),
			);

			if($id >0){
				$data['updatetime'] = time();
				$up = M('ping_ji')->where('id='.intval($id))->save($data);
			}else{
				$data['addtime'] = time();
				$up = M('ping_ji')->add($data);
			}

			//规格操作
			if($up){
				$this->redirect('pinglist',array('page'=>intval($_REQUEST['page'])));
				exit();
			}else{
				throw new \Exception('操作失败.');
			}
			  
		}
		if(!empty($id)){
			//查询该拼机信息
			$pingji_xq = M('ping_ji')->where('id='.intval($id))->find();
			$pingji_xq['start_time'] = date('Y-m-d H:i:s',$pingji_xq['start_time']);
		}
		//查询所有机场 
		$data = $this->ariport_xq();
		$pingji_xq['start_airport'] = $data[$pingji_xq['start_place']]['airport_name'];
		$pingji_xq['end_airport'] = $data[$pingji_xq['end_place']]['airport_name'];
		$airportdata = $this->airport_list_data();
		$this->assign('Feijidata',$Feijidata);
		$this->assign('data',$data);
		$this->assign('pingji_xq',$pingji_xq);
		
		$this->display();
	}

	//拼机列表
	public function pinglist(){
		//搜索
		$where="1=1 AND del<1 ";
		
		$count=M('ping_ji')->where($where)->count();
		define('rows',5);
		$rows=ceil($count/rows);
		$page=(int)$_GET['page'];
		$page<0?$page=0:'';
		$limit=$page*rows;
		$page_index=$this->page_index($count,$rows,$page);
		$productlist=M('ping_ji')->where($where)->order('addtime desc')->limit($limit,rows)->select();
		$data = $this->ariport_xq();
		foreach ($productlist as $k => &$vr) {
			$value['start_place'] = $vr['start_place'];
			$value['end_place'] = $vr['end_place'];
			$vr['start_place'] = $data[$value['start_place']]['address'];
			$vr['end_place'] = $data[$value['end_place']]['address'];
			$vr['start_airport'] = $data[$value['start_place']]['airport_name'];
			$vr['end_airport'] = $data[$value['end_place']]['airport_name'];
			$vr['addtime'] = date('Y-m-d',$vr['addtime']);
			$vr['start_time'] = date('Y-m-d',$vr['start_time']);
			$vr['end_time'] = date('Y-m-d',$vr['end_time']);
			if($vr['del'] ==1){
				$vr['status'] = '已删除';
			}else{
				$vr['status'] = '正常';
			}
			if($vr['mudi'] ==1){
				$vr['mudi'] = '商务';
			}elseif($vr['mudi'] ==2){
				$vr['mudi'] = '旅行';
			}elseif($vr['mudi'] ==3){
				$vr['mudi'] = '医疗';
			}else{
				$vr['mudi'] = '度假';
			}
		}

		//==========================
		// 将GET到的数据再输出
		//==========================
		if($_GET['test'] ==1){
			print_r($productlist);die;
		}
		
		//=============
		// 将变量输出
		//=============	
		$this->assign('pinglist',$productlist);
		$this->assign('page_index',$page_index);
		$this->display();
	}
	//拼机订单列表
	public function ping_orderlist(){
		
		//===============================
		// 产品列表信息 搜索
		//===============================
		//搜索
		$where="1=1 AND del<1 ";
		
		$count=M('ping_ji_order')->where($where)->count();
		define('rows',5);
		$rows=ceil($count/rows);
		$page=(int)$_GET['page'];
		$page<0?$page=0:'';
		$limit=$page*rows;
		$page_index=$this->page_index($count,$rows,$page);
		$productlist=M('ping_ji_order')->where($where)->order('addtime desc')->limit($limit,rows)->select();
		// dump($productlist);exit;
		foreach ($productlist as $k => &$vr) {
			$vr['addtime'] = date('Y-m-d',$vr['addtime']);
			$vr['start_time'] = date('Y-m-d',$vr['start_time']);
			$vr['end_time'] = date('Y-m-d',$vr['end_time']);
			if($vr['status'] ==1){
				$vr['status'] = '已付';
			}else{
				$vr['status'] = '未付';
			}
			if($vr['type'] ==1){
				$vr['type'] = '微信';
			}
		}

		//==========================
		// 将GET到的数据再输出
		//==========================
		if($_GET['test'] ==1){
			print_r($productlist);die;
		}
		
		//=============
		// 将变量输出
		//=============	
		$this->assign('ping_orderlist',$productlist);
		$this->assign('page_index',$page_index);
		$this->display();
	}
	//**********************************************
	//购机留言
	public function buy_message(){
		$where = "1=1";
		$count=M('buy_message')->where($where)->count();
		define('rows',5);
		$rows=ceil($count/rows);
		$page=(int)$_GET['page'];
		$page<0?$page=0:'';
		$limit=$page*rows;
		$page_index=$this->page_index($count,$rows,$page);
		$messagelist=M('buy_message')->where($where)->order('addtime desc')->limit($limit,rows)->select();
		$this->assign('messagelist',$messagelist);
		$this->assign('page_index',$page_index);
		$this->display();
	}
	//机型列表
	public function feiji_type(){
		$where = "1=1 and del <1";
		$count = M('feiji_type')->where($where)->count();
		define('rows',5);
		$rows=ceil($count/rows);
		$page=(int)$_GET['page'];
		$page<0?$page=0:'';
		$limit=$page*rows;
		$page_index=$this->page_index($count,$rows,$page);
		$feijilist=M('feiji_type')->where($where)->order('addtime desc')->limit($limit,rows)->select();
		$this->assign('feijilist',$feijilist);
		$this->assign('page_index',$page_index);
		$this->display();
	}
	//添加，修改机型
	public function add_feiji(){
		$name = $_POST['name'];//名称
		$id=(int)$_GET['did'];
		$content=$_POST['content'];//描述
		$number = $_POST['number'];//人数
		if(intval($id)>0){
			$pro_allinfo = M('feiji_type')->where('id='.intval($id))->find();
				
		}
		if($_POST['submit']==true){
			
			$array=array(
				'content'=>$content ,
				'name'=>$name ,
				'number' => $number,				
			);
			
			//执行添加
			if(intval($id)>0){
				
				
				//将空数据排除掉，防止将原有数据空置
				foreach ($array as $k => $v) {
					if(empty($v)){
					  	unset($v);
					}
				}

				$sql = M('feiji_type')->where('id='.intval($id))->save($array);
			}else{
				$array['addtime']=time();
				$sql = M('feiji_type')->add($array);

			}

			//规格操作
			if($sql){//name="guige_name[]
				$this->redirect('feiji_type',array('page'=>intval($_REQUEST['page'])));
				exit();
			}else{
				throw new \Exception('操作失败.');
			}
			  
		}
		$this->assign('id',$id);
		$this->assign('add_feiji',$pro_allinfo);
		$this->display();
	}
	//删除机型
	public function del(){
		$id = intval($_REQUEST['did']);
		$info = M('feiji_type')->where('id='.intval($id))->find();
		if (!$info) {
			$this->error('产品信息错误.'.__LINE__);
			exit();
		}

		if (intval($info['del'])==1) {
			$this->success('操作成功！.'.__LINE__);
			exit();
		}

		$data=array();
		$data['del'] = $info['del'] == '1' ?  0 : 1;
		$data['del_time'] = time();
		$up = M('feiji_type')->where('id='.intval($id))->save($data);
		if ($up) {
			$this->redirect('feiji_type',array('page'=>intval($_REQUEST['page'])));
			exit();
		}else{
			$this->error('操作失败.');
			exit();
		}
	}
	//机场列表
	public function airport_list(){
		$where = "1=1";
		$count=M('airport')->where($where)->count();
		define('rows',5);
		$rows=ceil($count/rows);
		$page=(int)$_GET['page'];
		$page<0?$page=0:'';
		$limit=$page*rows;
		$page_index=$this->page_index($count,$rows,$page);
		$feijilist=M('airport')->where($where)->order('addtime desc')->limit($limit,rows)->select();
		foreach($feijilist as &$vr){
			$vr['addtime'] = date('Y-m-d H:i:s',$vr['addtime']);
			if($vr['type'] == 1){
				$vr['type'] = '国内';
			}else{
				$vr['type'] = '国外';
			}
		}
		$this->assign('airport_list',$feijilist);
		$this->assign('page_index',$page_index);
		$this->display();
	}
	//机场信息
	public function ariport_xq(){
		$result = M('airport')->where()->field('id,airport_name,address')->select();
		foreach($result as $vr){
			$data[$vr['id']] = $vr;
		}

		return $data;
		// print_r($data);die;
	}
	//添加,编辑机场
	public function add_airport(){
		$id = $_GET['id'];
		$airport_name = $_POST['airport_name'];
		$type = $_POST['type'];
		$address = $_POST['address'];
		$pingyin = $_POST['pingyin'];
		$array = array(
			'airport_name' => $airport_name,
			'type' => $type,
			'address' => $address,
			'pingyin' => $pingyin,
		);
		if(intval($id)>0){
			$pro_allinfo = M('airport')->where('id='.intval($id))->find();
				
		}
		if($_POST['submit']==true){
			//执行添加
			if(intval($id)>0){
				
				
				//将空数据排除掉，防止将原有数据空置
				foreach ($array as $k => $v) {
					if(empty($v)){
					  	unset($v);
					}
				}

				$sql = M('airport')->where('id='.intval($id))->save($array);
			}else{
				$array['addtime']=time();
				$sql = M('airport')->add($array);

			}
			//规格操作
			if($sql){
				$this->redirect('airport_list',array('page'=>intval($_REQUEST['page'])));
				exit();
			}else{
				throw new \Exception('操作失败.');
			}
		}
		$pinyin = M('pingyin')->where()->select();
		$this->assign('add_airport',$pro_allinfo);
		$this->assign('pinyin',$pinyin);
		$this->display();
	}
	//删除机场
	public function del_airport(){
		$id = intval($_REQUEST['did']);
		$info = M('airport')->where('id='.intval($id))->find();
		if (!$info) {
			$this->error('产品信息错误.'.__LINE__);
			exit();
		}

		if (intval($info['del'])==1) {
			$this->success('操作成功！.'.__LINE__);
			exit();
		}

		$data=array();
		$data['del'] = $info['del'] == '1' ?  0 : 1;
		$data['del_time'] = time();
		$up = M('airport')->where('id='.intval($id))->save($data);
		if ($up) {
			$this->redirect('airport_list',array('page'=>intval($_REQUEST['page'])));
			exit();
		}else{
			$this->error('操作失败.');
			exit();
		}
	}
	//查询所有机场 
	public function airport_list_data(){
		$jcwhere = "1=1 and del <1 and type =1";
		$start_airdata = M('airport')->where($jcwhere)->select();
		$where = "1=1 and del <1 and type =2";
		$end_airdata = M('airport')->where($where)->select();
		$data['start'] = $start_airdata;
		$data['end'] = $end_airdata;
		return $data;
	}
	//查询机型信息
	public function feiji_xq(){
		$feiji_id = $_POST['feiji_id'];
		$data = M('feiji_type')->where('id='.intval($feiji_id))->find();
		
		echo json_encode($data);
	}	
	//分类列表
	public function catlist(){
		$where = "1=1";
		
		define('rows',5);
		$rows=ceil($count/rows);
		$page=(int)$_GET['page'];
		$page<0?$page=0:'';
		$limit=$page*rows;
		$page_index=$this->page_index($count,$rows,$page);
		$feijilist=M('feiji_cate')->where($where)->order('addtime desc')->limit($limit,rows)->select();
		foreach($feijilist as &$vr){
			$vr['addtime'] = date('Y-m-d H:i:s',$vr['addtime']);
			
		}
		$this->assign('catlist',$feijilist);
		$this->assign('page_index',$page_index);
		$this->display();
	}
	

	/*
	*
	* 添加或修改分类
	*/
	public function add_cat(){
		//如果是修改，则查询分类信息
		if (intval($_GET['id'])) {
			$id = intval($_GET['id']);
		
			$brand_info = M('feiji_cate')->where('id='.intval($id))->find();
			$this->assign('brand_info',$brand_info);
		}
		
		if($_POST['submit']==true){
			//上传广告图片
			$photo = '';
			if (!empty($_FILES["file"]["tmp_name"])) {
				//文件上传
				$info = $this->upload_images($_FILES["file"],array('jpg','png','jpeg'),'feijicate/'.date(Ymd));
			    if(!is_array($info)) {// 上传错误提示错误信息
			        $this->error($info);
			        exit();
			    }else{// 上传成功 获取上传文件信息
				    $photo = 'UploadFiles/'.$info['savepath'].$info['savename'];
				    
				    //生成国定大小的缩略图
				    /*$path_url = './Data/UploadFiles/'.$info['savepath'].$info['savename'];
				    $image = new \Think\Image();
				    $image->open($path_url);
				    $image->thumb(310, 120,\Think\Image::IMAGE_THUMB_FIXED)->save($path_url);*/
			    }
			}
			$data = array(
				'title' => $_POST['title'],
				'titleurl' => $photo,

			);
			// print_r($data);die;
			//保存数据
			if (intval($_POST['id'])) {
				$result = M('feiji_cate')->where('id='.intval($_POST['id']))->save($data);
			}else{
				//保存添加时间
				$data['addtime'] = time();
				$result = M('feiji_cate')->add($data);
			}
			//判断数据是否更新成功
			if ($result) {
				if (!empty($brand_info['titleurl']) && intval($_POST['id'])) {
					$img_url = "Data/".$brand_info['titleurl'];
					if(file_exists($img_url)) {
						@unlink($img_url);
					}
				}
				$this->success('操作成功.','catlist');
			}else{
				$this->error('操作失败.');
			}
		}
		$this->display();
	}

	/*
	*
	* 分类删除
	*/
	public function del_cat(){
		//获取广告id，查询数据库是否有这条数据
		$id = intval($_REQUEST['did']);
		$check_info = M('feiji_cate')->where('id='.intval($id))->find();
		if (!$check_info) {
			$this->error('参数错误！');
			die();
		}

		//修改对应的显示状态
		$up = M('feiji_cate')->where('id='.intval($id))->delete();
		if ($up) {
			if ($check_info['photo']) {
				$img_url = "Data/".$check_info['photo'];
				if(file_exists($img_url)) {
					@unlink($img_url);
				}
			}
			$this->success('操作成功.','catlist');
		}else{
			$this->error('操作失败.');
		}
	}
	
}